import threading
import socket


def sed_data(udp_socket, dest_ip, dest_port):
    # 发送数据
    while True:
        sedmsg = input('[+]输入要发送的数据:')
        sedmsg = sedmsg.encode('utf-8')
        # print(type(sedmsg))
        udp_socket.sendto(sedmsg, (dest_ip, dest_port))


def accept_data(udp_socket):
    # 接受数据
    while True:
        acceptmsg = udp_socket.recvfrom(1024)
        print(acceptmsg)


def main():
    # 创建套接字
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # 绑定本地
    udp_socket.bind(('', 7080))

    # 获取对方ip
    dest_ip = input('[+]输入ip:')
    dest_port = int(input('[+]输入端口:'))

    # 创建线程
    t_accept = threading.Thread(target=accept_data, args=(udp_socket,))
    t_sed = threading.Thread(target=sed_data, args=(udp_socket, dest_ip, dest_port))

    # 启动
    t_sed.start()
    t_accept.start()


if __name__ == '__main__':
    main()
